package com.zjw.zy.coreui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.zjw.zy.base.special.ActiveTaskPool;
import com.zjw.zy.utils.LogUtils;

/**
 * @author ：zhong.jw
 * @date ：Created in 2023/1/6 13:57
 */
public class TextSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private static final String TAG = "TextSurfaceView";
    private final ActiveTaskPool mSurfaceActiveTask = new ActiveTaskPool();
    private boolean isInit;
    private SurfaceHolder mSurfaceHolder;
    private int mFormat;
    private int mWidth;
    private int mHeight;
    private float mTextSize = 52f;
    private String mText = "";

    public TextSurfaceView(Context context) {
        super(context);

        onInit();
    }

    public TextSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

        onInit();
    }

    public TextSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        onInit();
    }

    public TextSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        onInit();
    }

    private void onInit() {
        if (isInit) {
            return;
        }

        isInit = true;

        LogUtils.d(TAG, "onInit");
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        LogUtils.d(TAG, "surfaceCreated");
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
        LogUtils.d(TAG, "surfaceChanged format = [" + format + "], width = [" + width + "], height = [" + height + "]");
        mFormat = format;
        mWidth = width;
        mHeight = height;
        mSurfaceHolder = holder;

        mSurfaceActiveTask.active();
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        LogUtils.d(TAG, "surfaceDestroyed");
        mSurfaceActiveTask.unActive();
        mSurfaceActiveTask.clear();
    }

    public void setText(@NonNull CharSequence text) {
        mText = text.toString();
        mSurfaceActiveTask.post(this::onDrawImpl);
    }


    private void onDrawImpl() {
        Canvas canvas = mSurfaceHolder.lockCanvas();
        //绘制背景
        canvas.drawColor(Color.BLACK);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextSize(mTextSize);

        //计算一行字数
        int count = computeLineLength(paint);

        char[] chars = mText.toCharArray();
        int maxLines = chars.length / count + 1;
        for (int i = 0; i < maxLines; i++) {
            canvas.drawText(chars, i * count, Math.min(count, chars.length - i * count), 0, 100f * (i + 1), paint);
        }

        mSurfaceHolder.unlockCanvasAndPost(canvas);
    }

    private int computeLineLength(Paint paint) {
        float v = paint.measureText(mText) / mText.length();
        return (int) ((mWidth * 1.0) / v);
    }
}
